﻿/* mvcForum
 * Copyright (C) 2010-2011 Steen F. Tøttrup
 * http://mvcforum.org/
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace mvcForum.Core {

	/// <summary>
	/// This class represent a forum.
	/// </summary>
	public class Forum {

		#region Constructors
		public Forum() { }
		public Forum(Category category, String name, Int32 sortOrder, String description) : this(category, name, sortOrder, description, null) { }
		public Forum(Category category, String name, Int32 sortOrder, String description, Forum parentForum) {
			this.Category = category;
			this.Name = name;
			this.SortOrder = sortOrder;
			this.Description = description;
			if (parentForum != null) {
				this.ParentForum = parentForum;
			}

			this.PostCount = 0;
			this.TopicCount = 0;
		}
		#endregion

		#region Properties
		/// <summary>
		/// The Id of the forum.
		/// </summary>
		public Int32 Id { get; set; }
		[Required]
		public Int32 CategoryId { get;set; }
		/// <summary>
		/// The category the forum belongs to.
		/// </summary>
		public virtual Category Category { get; set; }
		/// <summary>
		/// The name of the forum.
		/// </summary>
		[StringLength(Constants.FieldLengths.ForumName)]
		[Required]
		public String Name { get; set; }
		/// <summary>
		/// The sort order of the forum, used when visualising the bulletin board.
		/// </summary>
		[Required]
		public Int32 SortOrder { get; set; }
		public Int32? ParentForumId { get; set; }
		/// <summary>
		/// The parent forum. If this forum is not a sub-forum, this value is null.
		/// </summary>
		public virtual Forum ParentForum { get; set; }
		/// <summary>
		/// The description of this forum.
		/// </summary>
		[StringLength(Int32.MaxValue)]
		public String Description { get; set; }

		private DateTime? lastPosted;
		/// <summary>
		/// Timestamp of the latest post.
		/// </summary>
		public DateTime? LastPosted {
			get {
				return this.lastPosted;
			}
			set {
				this.lastPosted = value.Handle();
			}
		}
		public Int32? LastTopicId { get; set; }
		/// <summary>
		/// The topic in which the latest post was posted.
		/// </summary>
		public virtual Topic LastTopic { get; set; }
		public Int32? LastPostId { get; set; }
		/// <summary>
		/// The latest post.
		/// </summary>
		public virtual Post LastPost { get; set; }
		public Int32? LastPostUserId { get; set; }
		/// <summary>
		/// The author of the latest post.
		/// </summary>
		public virtual ForumUser LastPostUser { get; set; }
		/// <summary>
		/// The cached name of the author of the latest post.
		/// </summary>
		[StringLength(Constants.FieldLengths.Username)]
		public String LastPostUsername { get; set; }
		/// <summary>
		/// The number of topics in this forum.
		/// </summary>
		[Required]
		public Int32 TopicCount { get; set; }
		/// <summary>
		/// The number of posts in the topics of this forum.
		/// </summary>
		[Required]
		public Int32 PostCount { get; set; }
		#endregion

		public virtual ICollection<Forum> SubForums { get; set; }
		public virtual ICollection<ForumAccess> ForumAccesses { get; set; }
		public virtual ICollection<Topic> Topics { get; set; }
		public virtual ICollection<FollowForum> Followers { get; set; }
	}
}